package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.util.CollectionUtils;
import java.util.List;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private  WmChannelMapper channelMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;



    @Override
    public ResponseResult listAll() {
        return ResponseResult.okResult(this.list());
    }

    @Override
    public ResponseResult list(WmChannelDto dto) {
        //1.校验参数

        if (dto.getPage() == null || dto.getSize() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        QueryWrapper<WmChannel> queryWrapper = new QueryWrapper<>();
        if (dto.getStatus() != null){
            queryWrapper.eq("status",dto.getStatus());
        }
        if (dto.getName() != null && dto.getName() != ""){
            queryWrapper.like("name",dto.getName());
        }

//        IPage pageI = new Page(dto.getPage(), dto.getSize());
        Page page = new Page(dto.getPage(), dto.getSize());

        this.page(page,queryWrapper);

        //封装结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), (int) page.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult saveChannel(@RequestBody WmChannel wmChannel) {
        //判断参数是否为空
        if (StringUtils.isEmpty(wmChannel)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断名称是否重复
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper();
        wrapper.eq(WmChannel::getName, wmChannel.getName());
        WmChannel channel = channelMapper.selectOne(wrapper);
        if (!StringUtils.isEmpty(channel)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmChannel.setIsDefault(true);
        this.save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteChannel(Integer channelId) {
        System.out.println(channelId);
        if (channelId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //如果是禁用状态不能删除
        WmChannel wmChannel = channelMapper.selectById(channelId);
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (wmChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        this.removeById(channelId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(WmChannel wmChannel) {
        //判断参数是否为空
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 如果是禁用
        if (!wmChannel.getStatus()) {
            // 判断是否被引用
            List<WmNews> wmNewsList = wmNewsMapper.selectList(Wrappers.<WmNews>lambdaQuery()
                    .eq(wmChannel.getId() != null, WmNews::getChannelId, wmChannel.getId()));
            if (!CollectionUtils.isEmpty(wmNewsList)) {
                return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
            }
        }
        this.updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
